#coding:utf-8
import matplotlib.pyplot as plt
import numpy as np
from FileHelper import*

fg= '\\'

def get_loss_value():
    folder = r'F:\PaperCode\lw\result\f_r\loss\tensorboard'
    res1 = folder+fg+'loss_res1.npy'
    res3 = folder+fg+'loss_res3.npy'
    s1 = folder+fg+'loss_s1.npy'
    s3 = folder+fg+'loss_s3.npy'
    s1,s3 = np.load(s1),np.load(s3)
    res1,res3 = np.load(res1),np.load(res3)
    return s1,s3,res1,res3

def smooth_avg(array,k=5):
    for i in range(len(array)-5):
        array[i] = np.mean(array[i:i+5])
    return array

def display(array,xlabel,ylabel):
        plt.plot(array)
        plt.xlabel(xlabel)
        plt.ylabel(ylabel)
        plt.axis([0, 1000, 0, 2])
        plt.show()

def orgin_data_display():
    a,_,c,d = get_loss_value()
    # a,c,d = smooth_avg(a,2),smooth_avg(c,2),smooth_avg(d,2)
    xl = 'Iterations'
    yl = 'Loss'
    display(a,xl,yl)
    display(c,xl,yl)
    display(d,xl,yl)

# orgin_data_display()

def urk_fit_and_display():
    result = r'F:\PaperCode\lw\result\f_r\loss_pic.png'
    s1,s3,res1,res3 = get_loss_value()
    x_s1,x_s3 = np.arange(s1.size),np.arange(s3.size)
    x_res1,x_res3 = np.arange(res1.size),np.arange(res3.size)
    order = 9
    # c_s1,c_s3 = np.polyfit(x_s1,s1,order),np.polyfit(x_s3,s3,order)
    c_s1 = np.polyfit(x_s1,s1,order)
    c_res1,c_res3 = np.polyfit(x_res1,res1,order),np.polyfit(x_res3,res3,order)
    x = np.linspace(0, 950,100)
    # y_s1,y_s3 = np.polyval(c_s1,x),np.polyval(c_s3,x)
    y_s1 = np.polyval(c_s1,x)
    y_res1,y_res3 = np.polyval(c_res1,x),np.polyval(c_res3,x)
    plt.plot(x,y_s1,'r',label='U-Net')
    plt.plot(x,y_res1,'g+',label='UR-1')
    plt.plot(x,y_res3,'b--',label='UR-3')
    plt.xlabel('Iterations')
    plt.ylabel('Loss')
    plt.legend()
    plt.axis([0, 1000, 0, 2])
    plt.show()
    plt.savefig(result,dpi=100)

# urk_fit_and_display()

def list2str(L,func=None):
    r = ''
    for l in L:
        if func != None:
            l = func(l)
        r+=l
        r+=','
    return r[:-1]
def float_fix(d,num = 6):
    #输出固定小数点的浮点数
    r = '%.'+str(num)+'f'
    r = r%d
    return r

def dice_ur_csv_get():
    prefix = r'F:\PaperCode\lw\result\f_r\dice_'
    suffix='.npy'
    fn=['s1','res_s1','res_s3']
    result = r'F:\PaperCode\lw\result\f_r\dice.csv'
    content = ['index,s1,res_s1,res_s3']
    index = 44
    res = []
    for f in fn:
        data = np.load(prefix+f+suffix)[44:50]
        res.append(data)
    for a,b,c in zip(res[0],res[1],res[2]):
        r = str(index) +','+ float_fix(a)+','+float_fix(b)+','+float_fix(c)
        content.append(r)
        index += 1
    r = 'mean,'+float_fix(np.mean(res[0]))+','+float_fix(np.mean(res[1]))+','+float_fix(np.mean(res[2]))
    content.append(r)
    FileHelper.writelines(result,content)
 
# dice_ur_csv_get()

def arpf_ur_csv_get():
    prefix=r'F:\PaperCode\lw\result\f_r\arpf_'
    suffix1='.npy'
    suffix2 = '.csv'
    fn=['s1','res_s1','res_s3']
    content = ['index,Acc,Precision,Recall,F1']
    for f in fn:
        index = 44
        data = np.load(prefix+f+suffix1)[index:]
        for d in data:
            z,x,c,v = d
            r = str(index)+','+float_fix(z)+','+float_fix(x)+','+float_fix(c)+','+float_fix(v)
            content.append(r)
            index += 1
        endstr = 'mean,'+float_fix(np.mean(data[:,0]))+','+float_fix(np.mean(data[:,1]))+','+float_fix(np.mean(data[:,2]))+','+float_fix(np.mean(data[:,3]))
        content.append(endstr)
        FileHelper.writelines(prefix+f+suffix2,content)
        content = ['index,Acc,Precision,Recall,F1']

# arpf_ur_csv_get()

def dice_3d_csv_get():
    prefix = r'F:\PaperCode\lw\result\3d-ur\dice_'
    suffix='.npy'
    fn=['3^3','removeSkip']
    result = r'F:\PaperCode\lw\result\3d-ur\dice_3d.csv'
    content = ['index,3^3,removeSkip']
    index = 44
    res = []
    for f in fn:
        data = np.load(prefix+f+suffix)[4:]
        res.append(data)
    for a,b in zip(res[0],res[1]):
        r = str(index) +','+ float_fix(a)+','+float_fix(b)
        content.append(r)
        index += 1
    r = 'mean,'+float_fix(np.mean(res[0]))+','+float_fix(np.mean(res[1]))
    content.append(r)
    FileHelper.writelines(result,content)

# dice_3d_csv_get()

def arpf_3d_csv_get():
    prefix = r'F:\PaperCode\lw\result\3d-ur\arpf_'
    suffix1='.npy'
    suffix2 = '.csv'
    fn=['3^3','removeSkip']
    content = ['index,Acc,Precision,Recall,F1']
    for f in fn:
        index = 44
        data = np.load(prefix+f+suffix1)[4:]
        for d in data:
            z,x,c,v = d
            r = str(index)+','+float_fix(z)+','+float_fix(x)+','+float_fix(c)+','+float_fix(v)
            content.append(r)
            index += 1
        endstr = 'mean,'+float_fix(np.mean(data[:,0]))+','+float_fix(np.mean(data[:,1]))+','+float_fix(np.mean(data[:,2]))+','+float_fix(np.mean(data[:,3]))
        content.append(endstr)
        FileHelper.writelines(prefix+f+suffix2,content)
        content = ['index,Acc,Precision,Recall,F1']
arpf_3d_csv_get()
